﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;

namespace TranTekBE
{
    public partial class Category : PagePopUp
    {
        public int CatISN 
        { 
            get 
            {
                return FunctionsUtils.GetValueIntFromQueryString("isn", 0);
            } 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ltError.Text = string.Empty;

            if (this.Page.IsPostBack == false)
            {
                //this.CatISN = FunctionsUtils.GetValueIntFromQueryString("isn", -1);
                this.GetCategoryByISN(this.CatISN);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.CheckData() == true)
            {
                this.SaveCategory(this.CatISN, txtCategoryName.Text.Trim(), rblStatus.SelectedValue, txtComments.Text.Trim(), 0, 0);
            }
        }

        private bool CheckData()
        {
            bool bResult = true;

            if (txtCategoryName.Text.Trim().Length == 0)
            {
                FunctionsUtils.ShowSmallError(ltCategoryNameErr, FunctionsUtils.Msg["frm_Required"]);
                bResult = false;
            }

            return bResult;
        }

        private void SaveCategory(int CategoryISN, string catName, string catStatus, string catDesc, int ParentISN, int updatedBy)
        {            
            try
            {
                int iResult = (int)FunctionsUtils.Database.ExecuteStoredProcReturnObject("xp_category_insupd", new string[] { "CategoryISN", "catName", "catStatus", "catDesc", "ParentISN", "updatedBy" }, new object[] { CategoryISN, catName, catStatus, catDesc, ParentISN, updatedBy });
                if (iResult < 0)
                {
                    switch (iResult)
                    {
                        case -2: //catName exist
                            ltError.Text = string.Format(FunctionsUtils.Msg["frm_Exist"], "Category");
                            break;
                    }
                }
                else
                {
                    this.ClosePopupScripManager(true);
                }
            }
            catch (Exception ex)
            {
                ltError.Text = ex.Message;
                log.WriteLog("Error in Add or Edit Category: {0} {1}", ex, ex.StackTrace);
            }
        }

        private void GetCategoryByISN(int isn)
        {
            if (isn == 0)
                return;

            string sql = string.Empty;
            try
            {
                sql = string.Format("Select * From Category Where CategoryISN = {0}", isn);
                DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql);
                if (ds != null & ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    txtCategoryName.Text = ds.Tables[0].Rows[0]["catName"].ToString();
                    //rblStatus.s
                    FunctionsUtils.SelectItemInControlList(rblStatus, ds.Tables[0].Rows[0]["catStatus"].ToString(), SelectBy.Value);
                    txtComments.Text = ds.Tables[0].Rows[0]["catDesc"].ToString();
                }
                else
                {
                    ltError.Text = string.Format(FunctionsUtils.Msg["frm_NotExist"], "Category");
                }
            }
            catch (Exception ex)
            {
                ltError.Text = ex.Message;
                log.WriteLog("Error in Edit Category: {0} {1}", ex, ex.StackTrace);
            }
        }
    }
}
